
package com.dx.mes.service.query.service;


import static com.dx.mes.common.bean.def.GenericDef.E_FIPINQMTRL_INVALID_INPUT;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Ret_prd_mtdt;
import com.dx.mes.common.dao.IRetPrdMtdtDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.query.service.IFipinqmtrlService;
import com.dx.mes.facade.query.tx.Fipinqmtrl.FipinqmtrlI;
import com.dx.mes.facade.query.tx.Fipinqmtrl.FipinqmtrlO;
import com.dx.mes.facade.query.tx.Fipinqmtrl.FipinqmtrlOA;


@Service("fipinqmtrlService")
public class FipinqmtrlService implements IFipinqmtrlService {
    private LogUtils logUtils;
    private static final String TX_NAME = "FIPINQMTRL";

    @Autowired
    private IRetPrdMtdtDao retPrdMtdtDao;
    
    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(FipinqmtrlService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FipinqmtrlService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");

        String outMsg = "";
        FipinqmtrlI inTrx = new FipinqmtrlI();
        FipinqmtrlO outTrx = new FipinqmtrlO();

        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        try {
            inTrx = JacksonUtil.fromJson(strInMsg, FipinqmtrlI.class);
            char action_flg = inTrx.getAction_flg().charAt(0);
            switch (action_flg) {
	            case 'Q': {
	            	queryPrdMtrl(inTrx, outTrx);
	                break;
	            }
	            default: {
	                String gszMsgBuf = "Invalide type input: [" + action_flg + "]";
	                outTrx.setRtn_code(String.valueOf(E_FIPINQMTRL_INVALID_INPUT));
	                outTrx.setRtn_mesg(gszMsgBuf);
	            }
	        }
            if (!outTrx.getRtn_code().equals(RETURN_CODE_OK)) {
                TransactionAspectSupport.currentTransactionStatus()
                        .setRollbackOnly();
            } else {
            	retPrdMtdtDao.flushCurSession();
            }
        } catch (Exception ex) {
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logUtils.error(Util.stackTraceToString(ex));
            TransactionAspectSupport.currentTransactionStatus()
                    .setRollbackOnly();
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outTrx + "]");
        }
        return outMsg;
    }

    private void queryPrdMtrl(FipinqmtrlI inTrx, FipinqmtrlO outTrx) {
    	if(Util.isSpaceCheck(inTrx.getPrd_seq_id())){
            outTrx.setRtn_code(E_FIPINQMTRL_INVALID_INPUT + "");
            outTrx.setRtn_mesg("屏体代码不能为空");
            return;
    	}
    	List<FipinqmtrlOA> oaryList = new ArrayList<FipinqmtrlOA>();
    	//获取屏体绑定的物料信息(mtrl_mdl_stat=Y标识已经绑定的，N是之前绑定的)
    	List<Ret_prd_mtdt> ret_prd_mtdts = retPrdMtdtDao.find("FROM Ret_prd_mtdt WHERE prd_seq_id_fk='" + inTrx.getPrd_seq_id() 
    			+ "' AND mtrl_mdl_stat='Y'");
    	for(Ret_prd_mtdt ret_prd_mtdt:ret_prd_mtdts){
    		FipinqmtrlOA oary = new FipinqmtrlOA();
    		oary.setMtrl_cate(ret_prd_mtdt.getId().getMtrl_cate());
    		oary.setMtrl_prod_id_fk(ret_prd_mtdt.getId().getMtrl_prod_id_fk());
    		oary.setMtrl_batch(ret_prd_mtdt.getMtrl_batch());
    		oary.setMtrl_qty(ret_prd_mtdt.getMtrl_qty());
    		oaryList.add(oary);
    	}
    	outTrx.setOary(oaryList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

}
